home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr22 / dtkclk11.zip / CAL.C next >
Text File  |  1993-05-05  |  2KB  |  89 lines

  1. /*
  2.  * Calendar routines for DTK clock/calendar board handler.
  3.  * (C) Copyright 1989 Richard B. Wales.  All Rights Reserved.
  4.  */
  5.  
  6. #include "clkdefs.h"
  7.  
  8.  
  9. /*
  10.  * ADVANCE_TIME
  11.  *    Add a given number of seconds to a date/time.
  12.  */
  13. void advance_time (struct clockval *cv, int seconds)
  14. {
  15.     /*
  16.      * Perform sanity checking on the parameters.
  17.      */
  18.     if (cv == NULL_CLOCK) return;
  19.     if (seconds > 120 || seconds < 0) return;
  20.  
  21.     /*
  22.      * If the "seconds" increment is zero, there is nothing to do.
  23.      */
  24.     if (seconds == 0) return;
  25.  
  26.     /*
  27.      * Move the time forward by "seconds".
  28.      * Carry over into the rest of the time/date as needed.
  29.      * Assume "seconds" is no more than two minutes
  30.      * (i.e., carry-over is no more than one for hours and up).
  31.      */
  32.     cv->cv_second += seconds;
  33.     if (cv->cv_second < 60) return;
  34.     while (cv->cv_second >= 60)
  35.         cv->cv_minute++, cv->cv_second -= 60;
  36.     if (cv->cv_minute < 60) return;
  37.     cv->cv_hour++; cv->cv_minute -= 60;
  38.     if (cv->cv_hour < 24) return;
  39.     cv->cv_day++; cv->cv_hour = 0;
  40.     switch (cv->cv_month)
  41.     {    case 1: case 3: case 5: case 7:
  42.         case 8: case 10: case 12:
  43.             if (cv->cv_day <= 31) return;
  44.             break;
  45.         case 2:    if (cv->cv_day <=
  46.                 ((cv->cv_year & 0x3) ? 28 : 29))
  47.                 return;
  48.             break;
  49.         case 4: case 6: case 9: case 11:
  50.             if (cv->cv_day <= 30) return;
  51.             break;
  52.         default:return;        /* garbled date/time */
  53.     }
  54.     cv->cv_day = 1; cv->cv_month++;
  55.     if (cv->cv_month <= 12) return;
  56.     cv->cv_month = 1; cv->cv_year++;
  57.     if (cv->cv_year <= 99) return;
  58.     cv->cv_year = 0; cv->cv_century++;
  59. }
  60.  
  61.  
  62. /*
  63.  * DAY_OF_THE_WEEK
  64.  *      Determine the day of the week corresponding to a date
  65.  *    (0 = Sunday, 1 = Monday, . . ., 6 = Saturday).
  66.  */
  67. int day_of_the_week (struct clockval *cv)
  68. {    int answer, m, n;
  69.     static mtab[12] = { 1, 4, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6 };
  70.  
  71.     /*
  72.      * Perform sanity checking on the input parameter.
  73.      */
  74.     if (cv == NULL_CLOCK) return -1;
  75.  
  76.     /*
  77.      * Use the algorithm in Martin Gardner's _Mathematical Carnival_
  78.      * (Vintage Books, 1977; ISBN 0-394-72349-X), pp. 83-86.
  79.      */
  80.     n = cv->cv_year; m = n % 12;
  81.     answer = n/12 + m + m/4;
  82.     if (cv->cv_century == 20) answer += 6;
  83.     m = cv->cv_month - 1; answer += mtab[m];
  84.     answer += cv->cv_day; answer += 6;
  85.     if (m < 2 && (n & 0x3) == 0) answer += 6;
  86.     answer %= 7;
  87.     return answer;
  88. }
  89.